﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.WindowsEx;

namespace Krile.Forms.Dialogs.Show
{
    public partial class Reporter : Form
    {
        public Reporter()
        {
            InitializeComponent();
        }

        private void Reporter_Load(object sender, EventArgs e)
        {
            lists.Items.AddRange(Subsystem.Debugger.GetReports());
        }

        private void lists_SelectedIndexChanged(object sender, EventArgs e)
        {
            CopyBtn.Enabled = lists.SelectedIndex >= 0 && lists.Items.Count > 0;
            DelBtn.Enabled = CopyBtn.Enabled;
            IgnoreBtn.Enabled = CopyBtn.Enabled;
            if (lists.SelectedIndex >= 0)
                detailText.Text = lists.SelectedItem.ToString();
            else
                detailText.Text = String.Empty;
        }

        private void CopyBtn_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(lists.SelectedItem.ToString());
            MessageBoxEx.Show("Copied.", "Copy",
                 MessageBoxEx.MessageBoxExButtons.Close, MessageBoxEx.MessageBoxExIcon.Information);
        }

        private void DelBtn_Click(object sender, EventArgs e)
        {
            lists.Items.RemoveAt(lists.SelectedIndex);
            lists_SelectedIndexChanged(null, null);
        }

        private void IgnoreBtn_Click(object sender, EventArgs e)
        {
            var newItem = new Cores.Config._Security.ReportIgnoreData(lists.SelectedItem as Krile.Kernel.Bridges.DebugReport);
            List<Cores.Config._Security.ReportIgnoreData> items = new List<Krile.Cores.Config._Security.ReportIgnoreData>();
            if (Core.Config.Security.Ignores != null)
                items.AddRange(Core.Config.Security.Ignores);
            items.Add(new Cores.Config._Security.ReportIgnoreData(lists.SelectedItem as Krile.Kernel.Bridges.DebugReport));
            Core.Config.Security.Ignores = items.ToArray();
            List<object> listObj = new List<object>();
            foreach (var i in lists.Items)
            {
                if(!(i is Krile.Kernel.Bridges.DebugReport) ||
                    !newItem.IsIgnoreReport((Krile.Kernel.Bridges.DebugReport)i))
                {
                    listObj.Add(i);
                }
            }
            lists.Items.Clear();
            lists.Items.AddRange(listObj.ToArray());
            lists_SelectedIndexChanged(null, null);
        }

        private void Reporter_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (lists.Items.Count > 0)
            {
                foreach (var i in lists.Items)
                {
                    Subsystem.Debugger.AddReport(i as Krile.Kernel.Bridges.DebugReport);
                }
            }
        }

        private void DelAllBtn_Click(object sender, EventArgs e)
        {
            lists.Items.Clear();
            this.Close();
        }
    }
}
